package com.tracemap.websender.jsonparser;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class LocationJSON {

	private String phonenumber = "";
	private String latitude = "";
	private String longtitude = "";
	
	public LocationJSON(String phonenumber,String latitude,String longtitude)	{
        this.phonenumber = phonenumber;
        this.latitude = latitude;
        this.longtitude = longtitude;
    }
	
	public LocationJSON(JSONObject obj) throws JSONException {
        deserializeFromObj(obj);
    }

    public LocationJSON(String serializedObj) throws JSONException {
        deserialize(serializedObj);
    }

    public void deserialize(String serializedObj) throws JSONException {
        JSONObject obj = new JSONObject(serializedObj);
        deserializeFromObj(obj);
    }
    public void deserializeFromObj(JSONObject obj) throws JSONException {
        this.phonenumber = obj.getString("phonenumber");
        this.latitude = obj.getString("latitude");
        this.longtitude = obj.getString("longtitude");
        
    }

    public String serialize() throws JSONException {
        return serializeToObj().toString();
    }

    public JSONObject serializeToObj() throws JSONException {
        JSONObject serializedObj = new JSONObject();
        serializedObj.put("phonenumber", this.phonenumber);
        serializedObj.put("latitude", this.latitude);
        serializedObj.put("longtitude", this.longtitude);

        return serializedObj;
    }

    public static ArrayList<LocationJSON> deserializeArray(String serializedArray) throws JSONException {
        JSONArray jsonObjs = new JSONArray(serializedArray);
        ArrayList<LocationJSON> locations = new ArrayList<LocationJSON>();
        for (int i=0; i<jsonObjs.length(); i++) {
            JSONObject location = jsonObjs.getJSONObject(i);
            locations.add(new LocationJSON(location));
        }

        return locations;
    }
}
